package frame.options.simulation.results.sdn.numerical;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.components.networks.sdn.SdnNetwork;
import frame.options.simulation.results.ResultsFrame;

@SuppressWarnings("serial")
public class SdnNetworkTablesPanel extends JPanel implements ChangeListener
{
	private List< SdnNetwork > sdnNetworks;
	public List< SdnNetwork > getSdnNetworks(){ return sdnNetworks; }
	public void setSdnNetworks( List< SdnNetwork > sdnNetworks ){ this.sdnNetworks = sdnNetworks; }
	
	private ResultsFrame resultsFrame;
	public ResultsFrame getResultsFrame(){ return resultsFrame; }
	public void setResultsFrame( ResultsFrame resultsFrame ){ this.resultsFrame = resultsFrame; }
	
	private List< JScrollPane > sdnNetworkTablesScrollPanes;
	public List< JScrollPane > getSdnNetworkTablesScrollPanes(){ return sdnNetworkTablesScrollPanes; }
	public void setSdnNetworkTablesScrollPanes( List< JScrollPane > sdnNetworkTablesScrollPanes ){ this.sdnNetworkTablesScrollPanes = sdnNetworkTablesScrollPanes; }
	
	private JLabel label;
	public JLabel getLabel(){ return label; }
	public void setLabel( JLabel label ){ this.label = label; }
	
	private JSlider slider;
	public JSlider getSlider(){ return slider; }
	public void setSlider( JSlider slider ){ this.slider = slider; }
	
	private int sliderPreviousValue;
	public int getSliderPreviousValue(){ return sliderPreviousValue; }
	public void setSliderPreviousValue( int sliderPreviousValue ){ this.sliderPreviousValue = sliderPreviousValue; }
	
	public SdnNetworkTablesPanel( List< SdnNetwork > sdnNetworks, ResultsFrame resultsFrame )
	{
		this.setSdnNetworks( sdnNetworks );
		this.setResultsFrame( resultsFrame );
		
		this.setSdnNetworkTablesScrollPanes( this.sdnNetworkTablesScrollPanes( sdnNetworks ) );
		
		this.setLabel( new JLabel( sdnNetworks.get( 0 ).getName(), null, SwingConstants.CENTER ) );
		this.setSlider( this.slider( sdnNetworks.size() ) );
		this.setLayout( new BorderLayout() );
		this.add( this.getLabel(), BorderLayout.NORTH );
		this.add( this.getSdnNetworkTablesScrollPanes().get( 0 ), BorderLayout.CENTER );
		this.add( this.getSlider(), BorderLayout.SOUTH );
	}
	
	private List< JScrollPane > sdnNetworkTablesScrollPanes( List< SdnNetwork > sdnNetworks )
	{
		List< JScrollPane > sdnNetworkTablesScrollPanes = new ArrayList< JScrollPane >();
		
		for( SdnNetwork sdnNetwork: sdnNetworks )
			sdnNetworkTablesScrollPanes.add( new JScrollPane( new SdnNetworkTable( sdnNetwork ) ) );
		
		return sdnNetworkTablesScrollPanes;
	}
	
	private JSlider slider( int sdnNetworksNumber )
	{
		JSlider slider = new JSlider();
		slider.setPaintTicks( true );
		slider.setSnapToTicks( true );
		slider.setPaintTrack( true );
		slider.setPaintLabels( true );
		slider.setMinimum( 0 );
		slider.setMaximum( sdnNetworksNumber - 1 );
		slider.setMinorTickSpacing( 1 );
		slider.setMajorTickSpacing(	1 );
		slider.setValue( 0 );
		slider.addChangeListener( this );
		
		return slider;
	}
	
	@Override
	public void stateChanged( ChangeEvent changeEvent )
	{
		this.putValueSliders( this.getSlider().getValue() );
	}
	
	public void putValueSliders( int valueSlider )
	{
		this.setSliderValue( valueSlider );
		
		int selectedIndex = this.getResultsFrame().getSdnNetworksTablesPanelsTabbedPane().getSelectedIndex();
		this.getResultsFrame().getSdnNetworksViewersPanelsTabbedPane().setSelectedIndex( selectedIndex );
		this.getResultsFrame().getSdnNetworksViewersPanels().get( selectedIndex ).setSliderValue( valueSlider );
	}
	
	public void setSliderValue( int valueSlider )
	{
		SdnNetwork sdnNetwork = this.getSdnNetworks().get( valueSlider );
		
		if( sdnNetwork.getTimeStamp() == -1 )
			this.getLabel().setText( sdnNetwork.getName() );
		else
			this.getLabel().setText( "Time: " + sdnNetwork.getTimeStamp() );
		
		this.remove( this.getSdnNetworkTablesScrollPanes().get( this.getSliderPreviousValue() ) );
		this.setSliderPreviousValue( valueSlider );
		this.add( this.getSdnNetworkTablesScrollPanes().get( valueSlider ), BorderLayout.CENTER );
		this.getSlider().setValue( valueSlider );
		this.repaint();
	}
	
}
